using AppToolKit.Model.i18n;
using System;
using System.Data;
namespace AppToolKit.Model.Validation.Validator
{
	public class DomainRuleValidator : DataValidator
	{
		private string domain_;
		private string property_;
		public DomainRuleValidator(string domain, string property)
		{
			this.domain_ = domain;
			this.property_ = property;
		}
		public override ValidateResult Validate(ValidateContext context)
		{
			string commandText = string.Empty;
			if (context.NewValue is string || context.NewValue is DateTime)
			{
				commandText = string.Format("select count(*) from {0} where {1}='{2}'", this.domain_, this.property_, context.NewValue);
			}
			else
			{
				commandText = string.Format("select count(*) from {0} where {1}={2}", this.domain_, this.property_, context.NewValue);
			}
			int num = Convert.ToInt32(base.Database.ExecuteScalar(CommandType.Text, commandText));
			ValidateResult result;
			if (num == 1)
			{
				result = null;
			}
			else
			{
				result = ValidateResult.Error(string.Format(StringResource.DomainValidateMessage, context.PropertyDescript, context.NewValue, this.domain_));
			}
			return result;
		}
	}
}
